package edu.szte.ovrt.datahandler.ui.console;

import java.io.FileNotFoundException;

import edu.szte.ovrt.datahandler.core.*;
import edu.szte.ovrt.datahandler.model.Document;

/**
 * Entry point for the Console UI.
 * 
 * @author Péter Török
 */
public class MainMenu extends Menu implements edu.szte.ovrt.datahandler.ui.core.MainMenu {

	public MainMenu() {

		m_menuMap.add("Open", new IAction() {

			@Override
			public void run() throws ExitAction {
				final String fn = (new Dialog("Filename")).getValue();

				try {
					final IOFactory factory = IOMetaFactory.getInstance().getIOFactory(fn);
					if (factory == null) { throw new DocumentReaderException("File extension not supported!"); }
					final DocumentReader rd = factory.createDocumentReader();
					final Document doc = rd.readDocument();
					final EditDocument editor = new EditDocument(doc, fn);
					editor.run();
				} catch (final FileNotFoundException e) {
					showError("File not found!");
				} catch (final DocumentReaderException e) {
					if (e.getCause() != null) {
						showError(e.getCause().getMessage());
					} else {
						showError(e.getMessage());
					}
				}
			}
		});

		m_menuMap.add("New file", new IAction() {

			@Override
			public void run() throws ExitAction {
				new EditDocument(new Document()).run();
			}
		});

		m_menuMap.add("About", new IAction() {

			@Override
			public void run() throws ExitAction {
				System.out.println("Objektum Vezérelt Rendszerek Tervezése");
				System.out.println("2009/2010 őszi félév");
				System.out.println();
				System.out.println("GOT Team");
				System.out.println("Ambrus Péter, Papp Róbert, Török Péter");
			}
		});

		m_menuMap.add("Exit", new IAction() {

			@Override
			public void run() throws ExitAction {
				showMessage("Viszlát!");
				throw ExitAction.getInstance();
			}
		});

	}

	@Override
	protected String getTitle() {

		return "MAIN MENU";
	}
}
